<?php
namespace app\com\simtoon\publicmodels;
use Yii;
use yii\web\Link;
use yii\helpers\Url;
use yii\base\Exception;
use app\com\simtoon\log\LogUtils;
use app\com\simtoon\comm\Common;
use yii\data\SqlDataProvider;
use app\com\simtoon\utils\CacheUtils;
use app\com\simtoon\base\BaseModel;
use yii\data\ActiveDataProvider;

class Acceptance extends BaseModel{
	 //查询订单信息
        public function selectInfor($post){
        	//订单状态，预约送货时间，订单类型（送装，配送，安装），配送单号，配送人员头像、姓名、电话，配送车的类型，车牌号，起始位置，终点位置，是否已经验收，
        	 $querySql="select d.orderstatus,d.arrivetime as time,d.disnumstyle as style,d.distributionline as number,d.startplace,d.enddetail,if(dc.dname is null,i.cname,dc.dname) as name,if(dc.dphone is null ,i.xphone,dc.dphone)as phone,
                                     i.xctype,i.cnumber,d.carnumber from tms_sys_delivery  d left join t_car_infor i on d.carnumber=i.xcidfy left join t_dcar_infor dc on d.carnumber=dc.cidtf  where d.distributionline='".$post['id']."'  group by distributionline 
                                    union all 
                                     select p.orderstatus,p.distime as time,p.ltype as style,p.disnum as number,group_concat(s.provinceID,s.cityID,s.stock_address) as startplace,group_concat(c.provinceID,c.cityID,c.client_address)as enddetail,
                                    if(dc.dname is null,i.cname,dc.dname) as name,if(dc.dphone is null ,i.xphone,dc.dphone)as phone,i.xctype,i.cnumber,p.cnumber as carnumber
                                      from t_add_ttask p left join t_car_infor i on p.cnumber=i.xcidfy left join t_dcar_infor dc on p.cnumber=dc.cidtf left join t_sys_stockhouse s on p.stock_id=s.stock_id  left join  
                                     t_sys_client c on p.caddress=c.client_id where p.disnum='".$post['id']."' group by p.disnum";
             
  		$adp = array();
		try {
			LogUtils::info("Query sql={" . $querySql . "}", __METHOD__);
			$adp = new SqlDataProvider([
				'sql' => $querySql
				]);
		} catch (Exception $e) {
			LogUtils::error($e->getMessage(), __METHOD__);
			return $adp;
		} 
		return $adp;
        }
       //查询货品信息
       public function selectGood($post){
        	 //货品详情
        	$querySql="select goodname,goodnumber  from t_good_outgoods where listnumber='".$post['id']."'";
  		$adp = array();
		try {
			LogUtils::info("Query sql={" . $querySql . "}", __METHOD__);
			$adp = new SqlDataProvider([
				'sql' => $querySql
				]);
		} catch (Exception $e) {
			LogUtils::error($e->getMessage(), __METHOD__);
			return $adp;
		} 
		return $adp;
       }
       //保存用户提交的评价信息
       public function saveEvaluate($post){
            $record=array();
            $record['commentdate']=time();
            $record['disnum']=$post['number'];//订单编号
            $record['xcidfy']=$post['car'];//车组编号
            $data=$post['data'];
            //各个评价
            foreach($data as $key){
                 $record[$key['num']]=$key['answer'];//订单编号 
            }
            //保存数据
            $res=$this->execInsert('tms_sys_comment', $record);
            if($res > 0){
            	  //修改订单状态
            	       $sqlArr=array();
			$sqlArr[]="update tms_sys_delivery set orderstatus='4' where distributionline='".$post['number']."'";
			$sqlArr[]="update t_add_ttask set orderstatus='4' where disnum='".$post['number']."'";
			$update=$this->executeSqlBatch($sqlArr,3);
			if($update >0){
			     return array('result'=>'success');
			}else{
				$sql="delete from tms_sys_comment where disnum='".$post['number']."'";
				$this->executeSql($sql);
                           return array('result'=>'fail','msg'=>'信息提交失败，请稍后再试1');
			}
            }else{
                 return array('result'=>'fail','msg'=>'信息提交失败，请稍后再试');
            }
       }
       //查询是否已经抽奖,或该配送订单是否已经验收评价
       public function selectLcuk($post){
        	$querySql="select luck,disnum  from tms_sys_comment where disnum='".$post['id']."'";
  		$adp = array();
		try {
			LogUtils::info("Query sql={" . $querySql . "}", __METHOD__);
			$adp = new SqlDataProvider([
				'sql' => $querySql
				]);
		} catch (Exception $e) {
			LogUtils::error($e->getMessage(), __METHOD__);
			return $adp;
		} 
		return $adp;
       }
      //保存用户抽奖
      public function luckDraw($post){
           $id=rand(1,3);//获取随机抽奖结果
           $sql="update tms_sys_comment set luck='".$id."' where disnum='".$post['id']."'";
           $res=$this->executeSql($sql);
           if($res > 0){
                 return array('result'=>'success','id'=>$id);
            }else{
                 return array('result'=>'fail','msg'=>'系统繁忙，请稍后再试');
            }
      }
}
?>